import 'package:cms/pages/read/post_details/post_details_cubit.dart';
import 'package:cms/pages/read/post_details/post_details_state.dart';
import 'package:common/export/bloc.dart';
import 'package:common/export/tools.dart';
import 'package:common/utils/lara_press_date.dart';
import 'package:components/lara_press_app_bar.dart';
import 'package:components/lara_press_box.dart';
import 'package:components/lara_press_html.dart';
import 'package:components/lara_press_img.dart';
import 'package:components/lara_press_text.dart';
import 'package:flutter/material.dart';
import 'package:components/loaders/detail_loading.dart';

/// 文章详情页：原生flutter+html渲染
class LaraPressCmsReadNative extends StatelessWidget {
  final int id;
  const LaraPressCmsReadNative({super.key, required this.id});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => PostDetailsCubit(id: id),
      child: Builder(builder: (context) => _buildPage(context)),
    );
  }

  Widget _buildPage(BuildContext context) {
    return Scaffold(
      appBar: LaraPressAppBar(title: '文章详情'),
      body: BlocBuilder<PostDetailsCubit, PostDetailsState>(
        builder: (context, state) {
          if (state.isLoading) {
            return LaraPressBoxConvenience.card(
              child: LaraPressDetailLoading().wFull(context),
            );
          }
          return CustomScrollView(
            slivers: [
              SliverToBoxAdapter(
                child: LaraPressBoxConvenience.card(
                  borderRadius: 0,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.max,
                    spacing: 10.h,
                    children: [
                      LaraPressText(
                        text: state.postDetails?.title ?? '',
                        type: LaraPressTextType.detailTitle,
                      ),
                      Row(
                        spacing: 10.w,
                        children: [
                          LaraPressImg(
                            img: "${state.postDetails?.author?.avatar}",
                            width: 32.h,
                            height: 32.h,
                            radius: 32.h,
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            spacing: 2.h,
                            children: [
                              LaraPressText(
                                text: "${state.postDetails?.author?.name}",
                                type: LaraPressTextType.contentText,
                              ),
                              LaraPressText(
                                text:
                                    '发布于${LaraPressDate.formatToYMDHM(state.postDetails?.date?.created ?? '')}',
                                type: LaraPressTextType.listMeta,
                              ),
                            ],
                          ),
                        ],
                      ),
                      LaraPressHtml(html: state.postDetails?.content ?? ''),
                      Row(
                        spacing: 10.w,
                        children: [
                          LaraPressText(
                            text: "${state.postDetails?.actions?.views}次阅读",
                            type: LaraPressTextType.listMeta,
                          ),
                          LaraPressText(
                            text:
                                "最后修改时间：${LaraPressDate.formatToYMDHM(state.postDetails?.date?.modified ?? '')}",
                            type: LaraPressTextType.listMeta,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          );
        },
      ),
    );
  }
}
